package charles.testproject.guidemos;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

public class CardLayoutDemo extends JFrame {
	
	private final String names[] = {"RED","YELLOW","BLUE"};
	private final Color colors[] = {Color.RED,Color.YELLOW,Color.BLUE};
	private Button buttons[]   = new Button[3];
	private Panel centerPanel  = new Panel();
	private Panel northPanel   = new Panel();
	private Panel cardPanels[] = new Panel[3];
	private GridLayout gridLayout = new GridLayout(1,3);
	private CardLayout cardLayout = new CardLayout();
	
	ActionListener changeColorListener = new ActionListener() {

		public void actionPerformed(ActionEvent event) {
			Button button = (Button)event.getSource();
			cardLayout.show(centerPanel,button.getLabel());
		}
		
	};
	
	public CardLayoutDemo(String title) {
		super.setTitle(title);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		for(int i = 0; i < 3; i++) {
			buttons[i] = new Button(names[i]);
			buttons[i].addActionListener(changeColorListener);
			northPanel.setLayout(gridLayout);
			northPanel.add(buttons[i]);
			
			centerPanel.setLayout(cardLayout);
			
			cardPanels[i] = new Panel();
			cardPanels[i].setBackground(colors[i]);
			centerPanel.add(cardPanels[i],names[i]);
		}
		this.add(northPanel,BorderLayout.NORTH);
		this.add(centerPanel,BorderLayout.SOUTH);
		this.setSize(250,250);
		this.pack();
		this.setVisible(true);
	}
	public static void main(String[] args) {
		CardLayoutDemo gbld = new CardLayoutDemo("come on baby");
	}

}
